/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.storeChat.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.mnt.websocket.ChatSocket;
import me.zhengjie.modules.storeChat.domain.ChatMessages;
import me.zhengjie.modules.storeChat.service.ChatMessagesService;
import me.zhengjie.modules.storeChat.service.dto.ChatMessagesQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2023-11-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "storeChat管理")
@RequestMapping("/api/chatMessages")
public class ChatMessagesController {

    private final ChatMessagesService chatMessagesService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('chatMessages:list')")
    public void download(HttpServletResponse response, ChatMessagesQueryCriteria criteria) throws IOException {
        chatMessagesService.download(chatMessagesService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询storeChat")
    @ApiOperation("查询storeChat")
    @PreAuthorize("@el.check('chatMessages:list')")
    public ResponseEntity<Object> query(ChatMessagesQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(chatMessagesService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增storeChat")
    @ApiOperation("新增storeChat")
    @PreAuthorize("@el.check('chatMessages:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ChatMessages resources){
        return new ResponseEntity<>(chatMessagesService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改storeChat")
    @ApiOperation("修改storeChat")
    @PreAuthorize("@el.check('chatMessages:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ChatMessages resources){
        chatMessagesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除storeChat")
    @ApiOperation("删除storeChat")
    @PreAuthorize("@el.check('chatMessages:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        chatMessagesService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Autowired
    private ChatSocket chatSocket; // 注入 WebSocket 控制器

    @GetMapping("/getOnlineUsers")
    public Map<String, Object> getOnlineUsers() {
        Map<String, Object> result = new HashMap<>();
        Set<String> onlineUsers = chatSocket.getOnlineUsers(); // 获取在线用户列表
        result.put("onlineNum", onlineUsers.size());
        result.put("onlineList", new ArrayList<>(onlineUsers));
        return result;
    }

    @GetMapping("/getMessage/{sender}/{receiver}")
    @Log("查询storeChat")
    @ApiOperation("查询storeChat")
    public ResponseEntity<Object> getMessage(@PathVariable String sender,@PathVariable String receiver){
        return new ResponseEntity<>(chatMessagesService.getMessage(sender,receiver),HttpStatus.OK);
    }



}